package com.ymx.darling.session.setting.relation;

import com.ymx.darling.bean.entity.BeanInfo;
import com.ymx.darling.executor.mapping.relation.RelationFieldInfo;
import com.ymx.darling.util.CollectionUtil;

import java.util.LinkedList;
import java.util.List;

/**
 * 嵌套查询的实体
 */
public class RelationEntity {
    /**
     * 查询深度 默认为1
     */
    private int depth = 1;
    /**
     * 目标实体的BeanInfo
     */
    private final BeanInfo beanInfo;
    /**
     * 关联查询字段的信息
     */
    private RelationFieldInfo relationFieldInfo;
    /**
     * 待查询子节点列表
     */
    private final List<RelationEntity> queryPropertyList = new LinkedList<>();

    public RelationEntity(BeanInfo beanInfo) {
        this.beanInfo = beanInfo;
    }

    public void setDepth(int depth) {
        this.depth = depth;
    }

    public int getDepth() {
        return depth;
    }

    public boolean hasDepth() {
        return this.depth != 1;
    }

    public void addResultEntity(RelationEntity relationEntity) {
        this.queryPropertyList.add(relationEntity);
    }

    public BeanInfo getBeanInfo() {
        return beanInfo;
    }

    public List<RelationEntity> getQueryPropertyList() {
        return queryPropertyList;
    }

    public boolean hasAssociationQueryField() {
        return CollectionUtil.notEmpty(this.queryPropertyList);
    }

    public RelationFieldInfo getRelationFieldInfo() {
        return relationFieldInfo;
    }

    public void setRelationFieldInfo(RelationFieldInfo relationFieldInfo) {
        this.relationFieldInfo = relationFieldInfo;
    }

    public RelationEntity copy() {
        RelationEntity relationEntityCopy = new RelationEntity(this.getBeanInfo());
        relationEntityCopy.setDepth(depth);
        relationEntityCopy.setRelationFieldInfo(this.getRelationFieldInfo());
        this.getQueryPropertyList().forEach((resultEntity) -> relationEntityCopy.addResultEntity(resultEntity.copy()));
        return relationEntityCopy;
    }

    @Override
    public String toString() {
        return "RelationEntity{" +
                "depth=" + depth +
                ", beanInfo=" + beanInfo +
                ", relationFieldInfo=" + relationFieldInfo +
                ", queryPropertyList=" + queryPropertyList +
                '}';
    }
}
